# coding=utf-8

import datetime
import time
from MyExceptions import ParameterIllegalException


def get_during_seconds_from_now(days, hour=0, minute=0, second=0):
    """

    :param days:
    :param hour:
    :param minute:
    :param second:
    :return:
    """
    if hour > 23 or hour < 0:
        raise ParameterIllegalException("hour need in [0,23]")
    if minute > 59 or minute < 0:
        raise ParameterIllegalException("minute need in [0,59]")
    if second > 59 or second < 0:
        raise ParameterIllegalException("second need in [0,59]")
    if days == 0:
        if datetime.datetime.now().hour > hour:
            raise ParameterIllegalException("hour need large then %s" % (datetime.datetime.now().hour,))
    date_ex = (datetime.datetime.now() + datetime.timedelta(days=days))
    date_ex = date_ex.replace(hour=hour, minute=minute, second=second)
    during = time.mktime(date_ex.timetuple()) - time.mktime(
        datetime.datetime.now().timetuple())
    return int(during)


def get_during_seconds_from_now2(set_time=None):
    # set_time = '2019-1-9 23:40:00'
    # time_ex = datetime.datetime.strptime(set_time, "%Y-%m-%d %H:%M:%S")
    # time_ex = time_ex.replace(hour=0, minute=0, second=0)
    # # datetime.datetime.strptime(set_time, "%Y-%m-%d %H:%M:%S").minute = 0
    # print time_ex
    return (datetime.datetime.strptime(set_time, "%Y-%m-%d %H:%M:%S") - datetime.datetime.now()).total_seconds()


def get_during_days_to_now(set_time=None):
    # set_time = '2019-1-9 23:40:00'
    # time_ex = datetime.datetime.strptime(set_time, "%Y-%m-%d %H:%M:%S")
    # time_ex = time_ex.replace(hour=0, minute=0, second=0)
    # # datetime.datetime.strptime(set_time, "%Y-%m-%d %H:%M:%S").minute = 0
    # print time_ex
    return (datetime.datetime.now() - datetime.datetime.strptime(set_time,
                                                                 "%Y-%m-%d %H:%M:%S")).total_seconds() * 1.00 / 86400


def get_during_hours(b_time, e_time):
    return str((datetime.datetime.strptime(e_time.split('.')[0], "%Y-%m-%d %H:%M:%S") - datetime.datetime.strptime(
        b_time.split('.')[0], "%Y-%m-%d %H:%M:%S")).total_seconds() * 1.00 / 3600)


# print get_during_seconds_from_now(1)

def calc_exec_during_timing(policy_type, weekday, iter_time, long_time, point_time):
    ex_time = 0
    if policy_type == '1':
        if point_time:
            ex_time = get_during_seconds_from_now2(point_time)
            if ex_time <= 0:
                ex_time = 10
        else:
            ex_time = 10
    else:
        weekday_list = [d for d in weekday.split(';') if d != '']
        print weekday_list
        today = datetime.datetime.now().weekday()
        print today
        n_hour = datetime.datetime.now().hour + 1
        print n_hour
        b_hour = 0
        b_minute = 0
        b_second = 0
        if iter_time != '':
            h, m, s = iter_time.split('-')[0].rstrip(' ').split(':')
            b_hour = int(h)
            b_minute = int(m)
            b_second = int(s)
        jump_tag = True if b_hour < n_hour else False
        # if today in weekday:
        #     next_day_index = 0 if (weekday_list.index(str(today)) + 1) > (len(weekday_list) - 1) else (weekday_list.index(str(today)) + 1)
        #     during_day = (7+int(weekday_list[next_day_index])-today) if next_day_index == 0 else (int(weekday_list[next_day_index])-today)
        # else:
        bigger_list = [int(d) for d in weekday_list if today <= int(d)]
        print '0', bigger_list
        if len(bigger_list) >= 1:
            if today == bigger_list[0]:
                if jump_tag:
                    if len(bigger_list) == 1:
                        print '1'
                        during_day = 7 + int(weekday_list[0]) - today
                    else:
                        print '2'
                        during_day = bigger_list[1] - today
                else:
                    print '3'
                    during_day = 0
            else:
                print '4'
                during_day = bigger_list[0] - today
        else:
            print '5'
            during_day = 7 + int(weekday_list[0]) - today
        print during_day, b_hour, b_minute, b_second
        ex_time = get_during_seconds_from_now(during_day, b_hour, b_minute, b_second)
    return int(ex_time)


if __name__ == '__main__':
    t = str(datetime.datetime.now()).split('.')[0]
    print t
    time.sleep(2)
    print abs(int(get_during_seconds_from_now2(t)))
